package com.searshc.hadoop.migration.util;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringBufferInputStream;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.w3c.dom.Document;
import org.xhtmlrenderer.pdf.ITextRenderer;
import org.xml.sax.SAXException;

import com.itextpdf.text.DocumentException;


@Service("pdfFileGenerator")
public class PDFFileGenerator {
	private static Logger logger = Logger.getLogger("PDFFileGenerator");	
	public void createFile(Map<String, Map<String,String>> fileIssueMap){
		StringBuffer buf = new StringBuffer();
	    buf.append("<html>");	    
	    // put in some style
	    buf.append("<head><style language='text/css'>");
	    buf.append("h3 { border: 1px solid #aaaaff; background: #ccccff; ");
	    buf.append("padding: 1em; text-transform: capitalize; font-family: sansserif; font-weight: normal;}");
	    buf.append("p { margin: 1em 1em 4em 3em; } p:first-letter { color: red; font-size: 150%; }");
	    buf.append("h2 { background: #5555ff; color: white; border: 1px solid black; padding: 1em; font-size: 100%; }");
	    buf.append("</style></head>");
	    
	    // generate the body
	    buf.append("<body>");	
	    for(String scriptFile:fileIssueMap.keySet()){
	    	 buf.append("<tr></tr>");
	    	 buf.append("<tr></tr>");
		    buf.append("<tr>");
		    buf.append("<td><h2>Script File: "+ scriptFile+"</h2></td></tr>");
		    buf.append("<table border='1'>");
		    buf.append("<tr>");	    
		    buf.append("<td>Line Number</td>");
		    buf.append("<td>Issue With</td>");
		    buf.append("<td>Issue Description</td>");
		    buf.append("</tr>");
		    Map<String,String> issueMsgMap = fileIssueMap.get(scriptFile);
		    for(String issue:fileIssueMap.get(scriptFile).keySet()) {	    	
		    	buf.append("<tr>");	
		    	String[] issueLine = issue.split("\\|");
		 	    buf.append("<td>"+issueLine[0]+"</td>");
		 	    buf.append("<td>"+issueLine[1]+"</td>");
		 	    buf.append("<td>"+issueMsgMap.get(issue)+"</td>");
		 	    buf.append("</tr>");
		    }
		    buf.append("</table>");	   
	    }
	    buf.append("</body>");
	    buf.append("</html>");
	 // parse the markup into an xml Document
	    DocumentBuilder builder=null;
		try {
			builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		} catch (ParserConfigurationException e1) {
			logger.error("Error creating doc builder", e1);
		}
	    Document doc=null;
		try {
			doc = builder.parse(new StringBufferInputStream(buf.toString()));
		} catch (SAXException e1) {
			logger.error("Error creating document ", e1);
		} catch (IOException e1) {
			logger.error("Error creating document ", e1);
		}

	    ITextRenderer renderer = new ITextRenderer();
	    if(doc!=null){
		    renderer.setDocument(doc, null);
	
		    String outputFile = "pig_script_issues_report.pdf";
		    OutputStream os=null;
			try {
				os = new FileOutputStream(outputFile);
			} catch (FileNotFoundException e1) {
				logger.error("Error writing output ", e1);
			}
		    renderer.layout();
			try {
				renderer.createPDF(os);
			} catch (DocumentException e1) {
				logger.error("Error writing output ", e1);
			} catch (IOException e1) {
				logger.error("Error writing output ", e1);
			}
		    try {
				os.close();
			} catch (IOException e) {
				logger.error("Error closing the output stream ", e);
			}
	    }
	}
}
